﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using FeedDotNet.Common;

namespace ConsoleApplication
{
    class NzbIndexProvider : IFeedProvider
    {
        private Uri feedUri;
        private string key;
        public FeedItem feedItem;

        public NzbIndexProvider(FeedItem origItem, string query, string nfo, string minsize)
        {
            this.feedItem = origItem;

            feedUri = new Uri(
                string.Format(
                    "http://www.nzbindex.nl/rss/?q={0}&age=&max=4&sort=agedesc&minsize={1}&maxsize=&poster=&nfo={2}&hidespam=1&more=1", 
                    query.Replace("."," ").Substring(0,(query.IndexOf('.')<0?0:query.IndexOf('.'))), 
                    minsize, 
                    nfo)
                );
            key = query;
        }

        public Uri FeedUri
        {
            get { return feedUri; }
        }

        public string Key
        {
            get { return key; }
        }

        public FeedItem PopulateItemWithNzbLink(Feed nzbFeed)
        {
            if (nzbFeed == null)
            {
                return feedItem;
            }
            else
            {
                if (nzbFeed.Items.Count > 0)
                {
                    for (int i = 0; i < nzbFeed.Items.Count; i++)
                    {
                        if (nzbFeed.Items[i].Enclosures.Count > 0)
                        {
                            feedItem.Enclosures = nzbFeed.Items[i].Enclosures;
                            return feedItem;
                        }
                    }

                    return feedItem;
                }
                else 
                    return feedItem;
            }
        }

        private Feed _feed;
        public Feed FullFeed
        {
            get
            {
                return _feed;
            }
            set
            {
                _feed = value;
            }
        }

        public void PreProcess(ref Feed newFeed)
        {
            
        }

        public void PostProcess()
        {
        }
    }
}
